﻿using System.Collections.Generic;
using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class SearchContentPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly ISearchContentView _view;

        public SearchContentPresenter(ISearchContentView view)
            : this(view, new RepositoryFactory())
        {
        }

        public SearchContentPresenter(ISearchContentView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.SearchContent)
                _view.RedirectToPermissionDeniedPage();
        }

        public void Initialize()
        {
            SetLocales();
            _view.SetCategories(_administrator.CurrentSite.GetRootCategories());
            _view.SetMasterTemplates(_administrator.CurrentSite.MasterTemplates);
            _view.SelectedMasterTemplateID = _administrator.CurrentSite.DefaultMasterTemplate.ID;
            SearchContentSettings settings = _administrator.GetSearchContentSettings();
            if (settings != null)
            {
                _view.SelectedLocaleID = settings.LocaleID;
                _view.SelectedMasterTemplateID = settings.MasterTemplateID;
                _view.SelectedPageSize = settings.PageSize;
                _view.ShowPreviews = settings.ShowPreviews;
                _view.SortDirection = settings.SortDirection;
                _view.SelectedCategoryID = settings.ContentCategoryID;
            }
            SetContent(0, true);
        }
        
        public void SetLocales()
        {
            _view.SetLocales(_administrator.CurrentSite.Locales);
        }
        
        public void SetContent(int currentPage, bool rebuildFilter)
        {
            var settings = new SearchContentSettings
                               {
                                   ContentCategoryID = _view.SelectedCategoryID,
                                   ContentName = _view.ContentName,
                                   LocaleID = _view.SelectedLocaleID,
                                   MasterTemplateID = _view.SelectedMasterTemplateID,
                                   PageSize = _view.SelectedPageSize,
                                   SortMode = _view.SortMode,
                                   SortDirection = _view.SortDirection,
                                   ShowPreviews = _view.ShowPreviews
                               };
            _administrator.SetSearchContentSettings(settings);
            AdministratorRepository.SaveAdministrator(_administrator);
            var filter = new SearchContentFilter(_view.FilterState) { CurrentPage = currentPage };
            if (rebuildFilter)
            {
                filter.CategoryId = _view.SelectedCategoryID;
                filter.PageSize = _view.SelectedPageSize;
                filter.SortDirection = _view.SortDirection;
                filter.SortMode = _view.SortMode;
                filter.ContentName = _view.ContentName;
                filter.LocaleId = _view.SelectedLocaleID;
            }
            _view.FilterState = filter.GetQuery();
            Category selectedCategory = CategoryRepository.GetCategory(_view.SelectedCategoryID);
            IList<int> categoryIds = selectedCategory != null ? selectedCategory.GetAllIDs() : new List<int>();
            IList<Content> content = ContentRepository.SearchContent(ref filter, _administrator.CurrentSite.ID, categoryIds);
            _view.SetContentList(content, filter.Count, filter.CurrentPage, filter.PageSize);
        }
        
        public void Delete(int id)
        {
            ContentRepository.DeleteContent(id);
            var filter = new SearchContentFilter(_view.FilterState);
            SetContent(filter.CurrentPage, false);
        }
        
        public void ShareContent(int contentID, string areaID, string containerType, int containerID)
        {
            Content content = ContentRepository.GetContent(contentID);
            ContentSetContainer container = null;
            switch (containerType)
            {
                case "page":
                    container = PageRepository.GetPage(containerID);
                    break;
                case "mastertemplate":
                    container = SiteRepository.GetMasterTemplate(containerID);
                    break;
                case "detailtemplate":
                    container = SiteRepository.GetDetailTemplate(containerID);
                    break;
            }
            if (container == null)
                return;
            // Setup the link between container and content
            container.AddContentLink(areaID, content);
            // 
            foreach (var translation in content.Translations)
            {
                ContentSet set = container.GetContentSet(LocaleRepository.GetLocale(translation.Key));
                foreach (ContentTranslationVersion ctv in translation.Value.Versions)
                {
                    set.TranslationVersions.Add(ctv);
                }
            }
            SiteRepository.SaveSite(content.Site);
        }
    }
}